package com.jims.master.modules.drug.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.drug.entity.DrugExportDetail;
import com.jims.master.modules.drug.entity.DrugExportMaster;
import com.jims.master.modules.drug.entity.DrugInfo;
import com.jims.master.modules.drug.entity.DrugStock;
import com.jims.master.modules.drug.service.DrugExportService;
import com.jims.master.modules.drug.service.DrugStorageDeptService;
import com.jims.master.modules.sys.utils.UserUtils;
import com.jims.master.modules.utils.DrugUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 药品出库处理Controller
 * @author fengyg
 * @version 2017-03-16
 */
@Controller
@RequestMapping(value = "${adminPath}/drug/drugExport")
public class DrugExportController extends BaseController{

    @Autowired
    private DrugExportService drugExportService;
    @Autowired
    private DrugStorageDeptService drugStorageDeptService;


    @RequiresPermissions("user")
    @RequestMapping(value = {"index", ""})
    public String list(DrugExportDetail drugExportDetail, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<DrugExportDetail> page = drugExportService.findPage(new Page<DrugExportDetail>(request, response), drugExportDetail);
        model.addAttribute("page", page);
        return "modules/drug/drugExportIndex";
    }
    /**
     * 报损出库页面
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"drugBadIndex", ""})
    public String drugBadExportIndex(DrugExportDetail drugExportDetail, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<DrugExportDetail> page = drugExportService.findPage(new Page<DrugExportDetail>(request, response), drugExportDetail);
        model.addAttribute("page", page);
        return "modules/drug/drugBadExportIndex";
    }
    /**
     * 出库记录查询
     * @param drugExportMaster
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"query", ""})
    public String listque(DrugExportMaster drugExportMaster, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<DrugExportMaster> page = drugExportService.findExportList(new Page<DrugExportMaster>(request, response), drugExportMaster);
        Date date = drugExportMaster.getCreateDate();
        Date date1 = drugExportMaster.getUpdateDate();

        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (date != null) {
            drugExportMaster.setCreateDateStr(dateformat.format(date));
        }
        if (date1 != null) {
            drugExportMaster.setUpdateDateStr(dateformat.format(date1));
        }
        model.addAttribute("drugExportMaster",drugExportMaster);
        model.addAttribute("page", page);
        return "modules/drug/drugExportQueryIndex";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "form")
    public String form(DrugExportDetail drugExportDetail, Model model) {
        model.addAttribute("drugExportDetail", drugExportDetail);
        return "modules/drug/drugExportForm";
    }
    /**
     * @Desciption: 报损添加表格
     * @author ZYG
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "badForm")
    public String badForm(DrugExportDetail drugExportDetail, Model model) {
        model.addAttribute("drugExportDetail", drugExportDetail);
        return "modules/drug/drugBadExportForm";
    }
    /**
     * @param drugInfo 传递参数
     * @return List<DrugInfo> 返回类型
     * @throws
     * @Title: autoCompletePrice
     * @Desciption: (药品出库选择有库存有价格的药品自动补全)
     * @author fengyg
     * @date 2017-03-17
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"autoExportStock", ""})
    @ResponseBody
    public List<DrugExportDetail> autoExportStock(String q, String limit, DrugInfo drugInfo) {
        if (q != null && !q.equals("")) {
            try {
                q = URLDecoder.decode(q, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        List<DrugExportDetail> list = drugExportService.autoExportStock(drugInfo, UserUtils.getOrgId(), q, limit);
        return list;
    }

    /**
     * 药品出库处理
     * @param detailList
     * @param request
     * @param response
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "saveExport")
    @ResponseBody
    public StringData save(@RequestBody List<DrugExportDetail> detailList,HttpServletRequest request,HttpServletResponse response) {
        StringData stringData = new StringData();
        stringData = drugExportService.saveExportList(detailList);
        return stringData;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "delete")
    @ResponseBody
    public StringData delete(DrugExportDetail drugExportDetail) {
        StringData data = new StringData();
        //int i = DrugExportService.delete(drugExportMaster);
        //if (i > 0) {
        //    data.setCode("success");
        //    data.setData("删除药品出库成功");
        //} else {
        //    data.setCode("error");
        //    data.setData("删除药品出库失败");
        //}
        return data;
    }

    /**
     * 根据供货方ID(storageId)查询需要批量入库的数据
     * @param storageId 供货方ID
     * @param request
     * @param response
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"getBatchImportListData", ""})
    @ResponseBody
    public List<DrugStock> getBatchImportList(String storageId, HttpServletRequest request, HttpServletResponse response, Model model) {
        return drugExportService.getBatchExportList(storageId);
    }

    /**
     * 根据传递过来的入出库类别返回供货方集合
     * @param type     入出库类别,如果是退药，那么返回生产商供应商集合;如果是其他,返回库房集合
     * @param request
     * @param response
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"getSupplierList", ""})
    @ResponseBody
    public List<DrugExportDetail> getSupplierList(String type, HttpServletRequest request, HttpServletResponse response, Model model) {
        List list = DrugUtils.getSupplierOrStorageList(type);
        return list;
    }
}
